﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace GitMerger.Dto
{
    /// <summary>
    /// 配置
    /// </summary>
    public class GitSetting
    {
        /// <summary>
        /// 配置项
        /// </summary>
        public List<GitSettingItem> Commands { get; set; }

        public GitSetting()
        {
            this.Commands = new List<GitSettingItem>();
        }

        /// <summary>
        /// 从文件加载配置
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static GitSetting LoadFromFile(string path)
        {
            if (!egg.IO.CheckFileExists(path))
            {
                GitSetting setting = new GitSetting()
                {
                    Commands = new List<GitSettingItem>() {
                        new GitSettingItem() {
                            Name = "测试从 dev 合并到 main",
                            Folder = "Test",
                            Branch = "main",
                            ChildFolder = "Child",
                            SourceBranch = "dev",
                        },
                    },
                };
                egg.IO.WriteUtf8FileContent(path, JsonSerializer.Serialize(setting));
                return setting;
            }
            else
            {
                string json = egg.IO.ReadUtf8FileContent(path);
                return JsonSerializer.Deserialize<GitSetting>(json) ?? new GitSetting();
            }
        }
    }

    /// <summary>
    /// 配置项
    /// </summary>
    public class GitSettingItem
    {
        /// <summary>
        /// 名称
        /// </summary>
        public virtual string? Name { get; set; }

        /// <summary>
        /// 子目录
        /// </summary>
        public virtual string? Folder { get; set; }

        /// <summary>
        /// 模块目录
        /// </summary>
        public virtual string? ChildFolder { get; set; }

        /// <summary>
        /// 操作分支
        /// </summary>
        public virtual string? Branch { get; set; }

        /// <summary>
        /// 合并来源分支
        /// </summary>
        public virtual string? SourceBranch { get; set; }
    }
}
